package com.system.action;

import java.util.List;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.system.entity.DocType;
import com.system.service.DocTypeService;

public class DocTypeAction extends ActionSupport implements ModelDriven<DocType>{
	@Resource
	private DocTypeService docTypeService;
	private String message;
	private DocType docType;
	@Override
	public DocType getModel() {
		// TODO Auto-generated method stub
		if (docType == null) {
			docType = new DocType();
		}
		return docType;
	}
	
	public String doc_type_add() {
		if (docType.getType_name().equals("")) {
			setMessage("类型名称不可以为空");
			return ERROR;
		}
		docTypeService.add(docType);
		return SUCCESS;
	}
	
	public String queryAll() {
		List<DocType>docTypes = null;
		docTypes = docTypeService.queryAll();
		ActionContext.getContext().getSession().put("docTypes", docTypes);
		return SUCCESS;
	}
	
	public String doc_type_modify() {
		int type_id = Integer.parseInt(ServletActionContext.getRequest().getParameter("type_id"));
		DocType docType = docTypeService.queryById(type_id);
		ActionContext.getContext().getSession().put("docType", docType);
		return SUCCESS;
	}
	
	public String doc_type_delete() {
		docTypeService.delete(docType);
		return SUCCESS;
	}
	
	public String doc_type_update() {
		docTypeService.update(docType);
		return SUCCESS;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

}
